package com.chensf.studydemo.common

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.*
import android.widget.PopupWindow
import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chensf.studydemo.R
import kotlinx.android.synthetic.main.view_pop_window.view.*

object PopMenuHelper {
    fun showMenu(context: Context, anchor: View) {

        val layout = LayoutInflater.from(context).inflate(R.layout.view_pop_window, null)
        val recyclerView: RecyclerView = layout.recyclerView
        val list = mutableListOf(
            MenuAction("", R.drawable.chat_icon_duoxuan_default, ActionType.MultiCheck),
            MenuAction("", R.drawable.chat_icon_duoxuan_default, ActionType.MultiCheck),
//            MenuAction("", R.drawable.chat_icon_duoxuan_default, ActionType.MultiCheck),
//            MenuAction("", R.drawable.chat_icon_duoxuan_default, ActionType.MultiCheck),
            MenuAction("", R.drawable.chat_icon_duoxuan_default, ActionType.MultiCheck)
        )
        val count: Int = 5
        recyclerView.layoutManager = GridLayoutManager(context, count.coerceAtMost(list.size))
        recyclerView.adapter = MenuAdapter(list)

        layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
        val popWindow = PopupWindow(
            layout,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        popWindow.height =
            layout.measuredHeight.coerceAtMost((anchor.rootView.height * 0.9).toInt())
        popWindow.run {
            setBackgroundDrawable(ColorDrawable())
            isFocusable = true
            isOutsideTouchable = true
            inputMethodMode = PopupWindow.INPUT_METHOD_NOT_NEEDED
            softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING
        }

        showAtLocation(popWindow, anchor)
    }

    private fun showAtLocation(window: PopupWindow, anchor: View) {
        val location = IntArray(2)
        val rootView = anchor.rootView
        anchor.getLocationInWindow(location)
        //Y坐标
        if (location[1] >= window.contentView.measuredHeight
            + SizeUtils.dp2px(anchor.context, 80f)
        ) {//menu显示在statusBar下
            //author的y大于窗口高度时,向上弹出,窗口起点等于y-height
            location[1] = location[1] - window.height
            (window.contentView as MenuBubbleLayout).setDirection(MenuBubbleLayout.BOTTOM)
        } else if (location[1] + window.height > rootView.height) {
            //author的y加上window的高度大于rootView的高度时,窗口要居中在整个屏幕,起点等于整个界面高度/2-窗口高度/2
            location[1] = rootView.height / 2 - window.height / 2
            (window.contentView as MenuBubbleLayout).setDirection(MenuBubbleLayout.BOTTOM)
        } else {
            location[1] += anchor.height
            (window.contentView as MenuBubbleLayout).setDirection(MenuBubbleLayout.TOP)
        }
        val x = location[0] + anchor.width / 2

        val halfWindowWidth = when (window.width) {
            ViewGroup.LayoutParams.MATCH_PARENT -> {
                rootView.width / 2
            }
            ViewGroup.LayoutParams.WRAP_CONTENT -> {
                window.contentView.measuredWidth / 2
            }
            else -> {
                window.width / 2
            }
        }
        //X坐标
        if (x + halfWindowWidth > rootView.width) {
            //author的X加上window的宽度大于rootView的宽度时,窗口要居中在整个屏幕,起点等于整个界面宽度/2-窗口宽度/2
            location[0] = rootView.width - halfWindowWidth * 2
        } else if (x >= halfWindowWidth) {
            //author的x大于窗口宽度时,向左弹出,窗口起点等于x-halfWindowWidth
            location[0] = x - halfWindowWidth
        } else {
            location[0] = 0
        }
        (window.contentView as MenuBubbleLayout).setTriangleOffset(x - (location[0] + halfWindowWidth))
        window.showAtLocation(anchor, Gravity.NO_GRAVITY, location[0], location[1])
    }

    private class MenuAdapter(val list: List<MenuAction>) : RecyclerView.Adapter<MenuViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MenuViewHolder {
            return MenuViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.view_action_menu_item, parent, false)
            )
        }

        override fun onBindViewHolder(holder: MenuViewHolder, position: Int) {
        }

        override fun getItemCount(): Int {
            return list.size
        }

    }

    private class MenuViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}

data class MenuAction(val title: String, @DrawableRes val icon: Int, val actionType: ActionType)

enum class ActionType {
    Forward, Reply, MultiCheck, Revoke, Delete, Copy
}